package com.e.dianshang.controller.Api;

import com.alibaba.fastjson.JSON;
import com.e.dianshang.pojo.Address;
import com.e.dianshang.service.AddressService;
import com.e.dianshang.utils.JsonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@RestController
@CrossOrigin
@Api(value = "用户关注模块",tags = "用户关注api")
public class AddressApi {

    @Resource
    AddressService addressService;

    JsonResult jsonResult;

    @RequestMapping("selectAddress")
    @ApiOperation(value = "查询用户所有地址",httpMethod = "GET")
    public Map selectAddress(@RequestParam("openid")String openid){
        jsonResult=new JsonResult();
        List<Address> addresses = addressService.selectAddress(openid);
        if (addresses.isEmpty()){
            jsonResult.setMsg("还没有添加地址哦");
        }else {
            jsonResult.setData(addresses);
        }
        return jsonResult.getValues();
    }

    @RequestMapping("insertAddress")
    @ApiOperation(value = "用户添加地址",httpMethod = "POST")
    @ApiImplicitParam(name = "map",value = "地址表的所有信息",dataType = "map")
    public Map insert(@RequestBody Map map){
        jsonResult=new JsonResult();
        Address address = JSON.parseObject(JSON.toJSONString(map), Address.class);
        address.setState(true);
        Integer integer = addressService.insertAddress(address);
        if (integer>0){
            jsonResult.setCode("1");
            jsonResult.setMsg("添加成功");
        }else {
            jsonResult.setCode("0");
        }
        return jsonResult.getValues();
    }

    @RequestMapping("updateAddress")
    @ApiOperation(value = "用户修改地址信息",httpMethod = "POST")
    @ApiImplicitParam(name = "map",value = "地址的相关信息",dataType = "map")
    public Map update(@RequestBody Map map){
        jsonResult=new JsonResult();
        Address address = JSON.parseObject(JSON.toJSONString(map), Address.class);
        Integer integer = addressService.updateAddress(address);
        if (integer>0){
            jsonResult.setCode("1");
            jsonResult.setMsg("修改成功");
        }else {
            jsonResult.setCode("0");
        }
        return jsonResult.getValues();
    }

    @RequestMapping("deleteAddress")
    @ApiOperation(value = "用户删除地址信息",httpMethod = "POST")
    @ApiImplicitParam(name = "map",value = "用户电话或者用户id",dataType = "string")
    public Map delet(@RequestBody Map map){
        jsonResult=new JsonResult();
        String openid = map.get("openid").toString();
        Integer integer = addressService.deleteAddress(openid);
        if (integer>0){
            jsonResult.setCode("1");
            jsonResult.setMsg("删除成功");
        }else {
            jsonResult.setCode("0");
        }
        return jsonResult.getValues();
    }

    @RequestMapping("updateAddressState")
    @ApiOperation(value = "用户修改地址的默认状态",httpMethod = "POST")
    @ApiImplicitParam(name = "state",value = "地址默认状态",dataType = "boolean")
    public Map updateAddressState(@RequestBody Map map){
        jsonResult=new JsonResult();
        Address address = JSON.parseObject(JSON.toJSONString(map), Address.class);
        Integer integer = addressService.updateAddressState(address);
        if (integer>0){
            jsonResult.setCode("1");
            jsonResult.setMsg("修改成功");
        }else {
            jsonResult.setCode("0");
        }
        return jsonResult.getValues();
    }

}